package com.listeningframework.boot.autoconfigure.apidoc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Date;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * SwaggerConfiguration
 *
 * @author lixining
 * @version $Id: SwaggerConfiguration.java, v 0.1 2016年11月8日 下午4:17:47 lixining Exp $
 */
@Configuration
@EnableSwagger2
@ConditionalOnClass(Docket.class)
@ConditionalOnProperty(prefix = "listening.apidoc.swagger", name = "enabled", havingValue = "true", matchIfMissing = false)
public class SwaggerConfiguration {
    /**
     * 日志
     */
    private final Logger LOG = LoggerFactory.getLogger(SwaggerConfiguration.class);
    /**
     * Default API Route Pattern
     */
    private static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";

    /**
     * Swagger Springfox configuration.
     *
     * @param swaggerProperties - Swagger配置参数
     * @return Docket
     */
    @Bean
    public Docket swaggerSpringfoxDocket(SwaggerProperties swaggerProperties) {
        // @formatter:off
        LOG.debug("Starting Swagger");
        StopWatch watch = new StopWatch();
        watch.start();
        Contact contact = new Contact(
            swaggerProperties.getSwagger().getContactName(),
            swaggerProperties.getSwagger().getContactUrl(),
            swaggerProperties.getSwagger().getContactEmail());

        ApiInfo apiInfo = new ApiInfo(
            swaggerProperties.getSwagger().getTitle(),
            swaggerProperties.getSwagger().getDescription(),
            swaggerProperties.getSwagger().getVersion(),
            swaggerProperties.getSwagger().getTermsOfServiceUrl(),
            contact,
            swaggerProperties.getSwagger().getLicense(),
            swaggerProperties.getSwagger().getLicenseUrl());

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo)
            .forCodeGeneration(true)
            .genericModelSubstitutes(ResponseEntity.class)
            .ignoredParameterTypes(java.sql.Date.class)
            .directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
            .directModelSubstitute(java.time.ZonedDateTime.class, Date.class)
            .directModelSubstitute(java.time.LocalDateTime.class, Date.class)
            .select()
            .paths(regex(DEFAULT_INCLUDE_PATTERN))
            .build();
        watch.stop();
        LOG.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
        return docket;
        // @formatter:on
    }

    //    @Bean
    //    @Primary
    //    public DefaultSwaggerResourcesProvider getDefaultSwaggerResourcesProvider() {
    //        return new DefaultSwaggerResourcesProvider();
    //    }

}
